package com.bridge.bridge_app;

import android.content.Intent;
import android.graphics.Color;
import android.view.KeyEvent;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bridge.baselibs.rxpremissions.PermissionsUtils;
import com.bridge.bridge_app.databinding.AcHomeBinding;
import com.bridge.common.adapter.FragmentViewPagerAdapter;
import com.bridge.common.base.BaseActivity;
import com.bridge.common.route.RouteUtils;
import com.bridge.common.utils.ToastshowUtils;
import com.lqr.wechat.broadcast.AuthBroadcastReceiver;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.StatusCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import devlight.io.library.ntb.NavigationTabBar;

/**
 * author : bridge.24
 * date   : 21.3.115:33
 * desc   :
 */
@Route(path = "/bridge_app/HomeActivity")
public class HomeActivity extends BaseActivity {
    private AcHomeBinding binding ;
    private long mExitTime;
    // 定义数组来存放Fragment界面
    private final Fragment[] mFragmentArray = RouteUtils.getTabFragment();
    // Tab选项卡的文字
    private final int[] mTextArray = {R.string.str_hh, R.string.str_txl, R.string.str_ts, R.string.str_fx,R.string.str_wd,};
    //正常图片
    private final int[] mImageNormalArray = {R.mipmap.ic_logo, R.mipmap.ic_logo, R.mipmap.ic_logo, R.mipmap.ic_logo, R.mipmap.ic_logo};
    private AuthBroadcastReceiver mAuthBroadcastReceiver;
    private Observer<StatusCode> mOnlineStatusObserver;

    @Override
    protected int layoutId() {
        return R.layout.ac_home;
    }

    @Override
    protected void initView() {
        binding = getBind() ;
        List<String> titles = new ArrayList<>();
        ArrayList<NavigationTabBar.Model> modelList = new ArrayList<>();
        for (int index = 0; index < mTextArray.length; index++) {
            titles.add(getString(mTextArray[index]));
            modelList.add(new NavigationTabBar.Model.Builder(
                    ContextCompat.getDrawable(this ,mImageNormalArray[index]), Color.TRANSPARENT)
                    .title(getString(mTextArray[index]))
                    .build()
            );
        }
        FragmentViewPagerAdapter adapter = new FragmentViewPagerAdapter(getSupportFragmentManager(), Arrays.asList(mFragmentArray), titles);
        binding.tabBar.setActiveColor(getResources().getColor(R.color.main_navigation_tab_bar_item_selected));//动态代码设置
        binding.tabBar.setInactiveColor(getResources().getColor(R.color.main_navigation_unselected));
        binding.tabBar.setModels(modelList);
        //binding.tabBar.setModelIndex(INDEX_HOME,true);//跟adapter index冲突
        binding.mainPager.setAdapter(adapter);
        binding.tabBar.setViewPager(binding.mainPager);
        PermissionsUtils.requestAllPermissions(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        for (Fragment fragment:mFragmentArray) {
            fragment.onActivityResult(requestCode,resultCode,data);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                ToastshowUtils.showToastSafe("再按一次退出程序");
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                application.removeALLActivity_();//执行移除所以Activity方法
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void setListenner() {
        binding.tabBar.setOnTabBarSelectedIndexListener(new NavigationTabBar.OnTabBarSelectedIndexListener() {
            @Override
            public void onStartTabSelected(NavigationTabBar.Model model, int index) {
                //Log.d(TAG, "onStartTabSelected: ");
            }

            @Override
            public void onEndTabSelected(NavigationTabBar.Model model, int index) {
                //Log.d(TAG, "onEndTabSelected: ");
            }
        });
        binding.tabBar.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //Log.d(TAG, "onPageScrolled: ");
            }

            @Override
            public void onPageSelected(int position) {
                //Log.d(TAG, "onPageSelected: ");
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //Log.d(TAG, "onPageScrollStateChanged: ");
            }
        });
    }
}
